package com.lanyun.controller;

import com.lanyun.common.R;
import com.lanyun.dao.intf.TouruserDao;
import com.lanyun.entity.TouruserEntity;
import com.lanyun.entity.vo.UserQuery;
import com.lanyun.mapper.TouruserMapper;
import com.lanyun.wrapper.TouruserQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @author: LanYun
 * @date: 2022/3/21 16:53
 * @Version: 1.0
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private TouruserDao touruserDao;

    @Autowired
    @Qualifier("touruserMapper")
    private TouruserMapper touruserMapper;

    @PostMapping("/login")
    public R login(@RequestBody String info){

        touruserDao.login(info);
        return R.ok();
    }

    @PostMapping
    public R list(@RequestBody UserQuery userQuery){
        TouruserQuery touruserQuery = new TouruserQuery();
        if(!"".equals(userQuery.getKey())){
            touruserQuery.where().nickName().like(userQuery.getKey());
        }
        List<TouruserEntity> touruserEntities = touruserMapper.listEntity(touruserQuery);
        return R.ok().data("rows",touruserEntities);
    }

    @GetMapping("/{id}")
    public R getOne(@PathVariable("id")Long id){
        TouruserEntity byId = touruserMapper.findById(id);
        return R.ok().data("user",byId);
    }

    @PostMapping("/updateUserById")
    public R update(@RequestBody TouruserEntity touruserEntity){
        touruserMapper.saveOrUpdate(touruserEntity);
        return R.ok();
    }
}
